//
//  MOFSDatePicker.m
//  MOFSPickerManager
//
//  Created by luoyuan on 16/8/26.
//  Copyright © 2016年 luoyuan. All rights reserved.
//

#import "MOFSDatePicker.h"

#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface MOFSDatePicker()

@property (nonatomic, strong) NSMutableDictionary *recordDic;
@property (nonatomic, strong) UIView *bgView;

@end


@implementation MOFSDatePicker

- (NSMutableDictionary *)recordDic {
    if (!_recordDic) {
        _recordDic = [NSMutableDictionary dictionary];
    }
    return _recordDic;
}

#pragma mark - create UI

- (instancetype)initWithFrame:(CGRect)frame {
    
    //[self initToolBar];
    //[self initContainerView];
    
    CGRect initialFrame;
    
    if (CGRectIsEmpty(frame)) {
        
        initialFrame = CGRectMake(0, 0, UISCREEN_WIDTH, SCREEN_HEIGHT);
        
    } else {
        
        initialFrame = frame;
    }
    
    self = [super initWithFrame:initialFrame];
    
    if (self) {
        
        [self initPickerView];
        
        [self initContainerView];
        
        [self initBgView];
        
        self.backgroundColor = CSClearColor;
        
        self.pickerView.datePickerMode = UIDatePickerModeDate;
        
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        //[self initBgView];
    }
    return self;
}

- (void)initToolBar {
    self.toolBar = [[MOFSToolbar alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 44)];
    self.toolBar.translucent = NO;
}

- (void)initContainerView {
    
    self.containerView = [[UIView alloc] init];
    
    self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    
    self.containerView.userInteractionEnabled = YES;
    
    [self.containerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenWithAnimation)]];
    
    [self addSubview:self.containerView];
    
    [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

- (void)initBgView {
    
    //白色背景
    self.bgView = [[UIView alloc] init];
    self.bgView.backgroundColor = CSWhiteColor;
    [self addSubview:self.bgView];
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
        make.left.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-20);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = BoldBigFont;
    [self.bgView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.bgView).offset(20);
        make.centerX.mas_equalTo(self.bgView);
    }];
    
    UIButton *closeBtn = [[UIButton alloc] init];
    [closeBtn setImage:@"pop_icon_close"];
    [closeBtn addTarget:self action:@selector(hiddenWithAnimation)];
    [self.bgView addSubview:closeBtn];
    self.closeBtn = closeBtn;
    [closeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.bgView).offset(-10);
        make.top.mas_equalTo(self.bgView).offset(10);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = LineBackGroundColor;
    [self.bgView addSubview:lineView];
    self.lineView = lineView;
    [lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(30);
        make.left.width.mas_equalTo(self.bgView);
        make.height.mas_equalTo(1);
    }];
    
    [self.bgView addSubview:self.pickerView];
    [self.pickerView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lineView.mas_bottom);
        make.left.width.mas_equalTo(self.bgView);
        make.height.mas_equalTo(200);
    }];
    
    MOFSBottomView *bottomView = [[MOFSBottomView alloc] init];
    [self.bgView addSubview:bottomView];
    self.bottomView = bottomView;
    [bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.pickerView.mas_bottom);
        make.left.width.bottom.mas_equalTo(self.bgView);
    }];
    
}

-(void)initPickerView{
    
    // 初始化pickerView
    self.pickerView = [[UIDatePicker alloc]init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
    self.pickerView.locale = locale;
    //[self.bgView addSubview:self.pickerView];
    
    
    //指定数据源和委托
    //self.pickerView.delegate = self;
    //self.pickerView.dataSource = self;
    
}

#pragma mark - Action

- (void)showMOFSDatePickerViewWithTag:(NSInteger)tag firstDate:(NSDate *)date commit:(CommitBlock)commitBlock cancel:(CancelBlock)cancelBlock {
    
     NSString *showtagStr = [NSString stringWithFormat:@"%ld",(long)tag];
    
    if ([self.recordDic.allKeys containsObject:showtagStr]) {
        NSDate *date1 = self.recordDic[showtagStr][showtagStr];
        self.pickerView.date = date1;
    } else {
        if (date) {
            self.pickerView.date = date;
        } else {
            self.pickerView.date = [NSDate date];
        }
    }
    
    [self showWithAnimation];
    __weak __typeof(self) weakSelf = self;
    
    self.bottomView.cancelBlock = ^{
        [weakSelf hiddenWithAnimation];
        if (cancelBlock) {
            cancelBlock();
        }
    };
    
    self.bottomView.commitBlock = ^{
       
        NSDictionary *dic = [NSDictionary dictionaryWithObject:weakSelf.pickerView.date forKey:showtagStr];
        
        [weakSelf.recordDic setValue:dic forKey:showtagStr];
        
        [weakSelf hiddenWithAnimation];
        
        if (commitBlock) {
            
            commitBlock(weakSelf.pickerView.date);
        }
    };

}

- (void)showWithAnimation {
    
    [self addViews];
    
    self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    
    CGFloat height = self.bgView.frame.size.height;
    
    self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT + height / 2);
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT - height / 2);
        
        self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
    }];
    
}

- (void)hiddenWithAnimation {
    
    CGFloat height = self.bgView.frame.size.height;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.bgView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT + height / 2);
        
        self.containerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
        
    } completion:^(BOOL finished) {
        
        [self hiddenViews];
    }];
}

- (void)addViews {
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    [window addSubview:self];
}

- (void)hiddenViews {
    
    [self removeFromSuperview];
//    [self.toolBar removeFromSuperview];
//    [self.bgView removeFromSuperview];
//    [self.containerView removeFromSuperview];
}


@end
